package com.example.studentmanagement2.repository.JdbcRepository;

import com.example.studentmanagement2.model.Student;
import com.example.studentmanagement2.model.Teacher;
import com.example.studentmanagement2.repository.Repository.StudentRepository;
import com.example.studentmanagement2.repository.Repository.TeacherRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public class JdbcTeacherRepository implements TeacherRepository {

    private final JdbcTemplate jdbcTemplate;


    public JdbcTeacherRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Teacher> findAll() {
        return jdbcTemplate.query(
                "SELECT * FROM teachers",
                (rs, rowNum) -> new Teacher(//rs: 表示查询结果集，包含从数据库中检索到的数据。
                        //rowNum: 表示当前行的编号，从0开始。
                        rs.getString("id"),
                        rs.getString("name")

                )
        );
    }

    @Override
    public Optional<Teacher> findById(String id) {//Optional是Java 8引入的一个容器类，用于表示可能为空的值。
        try {
            return jdbcTemplate.queryForObject(
                    "SELECT * FROM teachers WHERE id = ?",
                    new Object[]{id},//查询参数，将id值绑定到SQL语句中的问号占位符。可以存储任何类型的对象：
                    (rs, rowNum) -> Optional.of(new Teacher(
                            rs.getString("id"),
                            rs.getString("name")

                    ))
            );
        } catch (EmptyResultDataAccessException e) {
            return Optional.empty(); // 显式处理空结果
        }
    }

    @Override
    public Teacher save(Teacher teacher) {
        jdbcTemplate.update(
                "INSERT INTO teachers (id, name) VALUES (?, ?)",
                teacher.getId(), teacher.getName()
        );
        return teacher;
    }

    @Override
    public void update(Teacher teacher, String originalId) {  // 新增参数 originalId

        jdbcTemplate.update(

                "UPDATE teachers SET id=?, name=? WHERE id=?",
                teacher.getId(),     // 新学号
                teacher.getName(),

                originalId          // 原始学号（WHERE 条件）
        );
    }

    @Override
    public void deleteById(String id) {
        jdbcTemplate.update("DELETE FROM teachers WHERE id = ?", id);
    }

    @Override
    public boolean existsById(String id) {
        String sql = "SELECT COUNT(*) FROM teachers WHERE id = ?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, id);
        return count != null && count > 0;
    }
    @Override
    public List<Teacher> search(String keyword) {
        String sql = "SELECT * FROM teachers WHERE " +
                "id LIKE ? OR name LIKE ? ";
        String pattern = "%" + keyword + "%";
        return jdbcTemplate.query(
                sql,
                new Object[]{pattern, pattern},
                (rs, rowNum) -> new Teacher(
                        rs.getString("id"),
                        rs.getString("name")

                )
        );
    }






}